import { computed } from 'vue'
import { useStore } from "vuex";
import { toRefs } from 'vue'
//购物车相关逻辑
export const useCommonCartEffect = (shopId) => {
    const store = useStore();
    const { cartList } = toRefs(store.state);
    const changeCartItemInfo = (shopId, productId, productInfo, num) => {
      //commmit发出去触发store的addItemToCart
      store.commit("changeCartItemInfo", {
        shopId,
        productId,
        productInfo,
        num,
      });
      console.log(shopId, productId, productInfo, num);
    };

    const productList = computed(() => {
      const productList = cartList[shopId] || {};
      const notEmptyProductList = {}
      for(let i in productList){
        const product = productList[i]
        if(product.count >0){
          notEmptyProductList[i] = product
        }
      }
      console.log(notEmptyProductList)
      return notEmptyProductList;
    });

    const shopName = computed(() =>{
      const shopName = cartList[shopId]?.shopName || ''
      return shopName
    })

    return { cartList, shopName,productList, changeCartItemInfo };
  };